import { _decorator, BoxCollider2D, Component, ERigidBody2DType, RigidBody2D, TiledMap, v2, Node } from 'cc';

import { Dialog } from './Dialog'

const { ccclass, property } = _decorator;

@ccclass('Game')
export class Game extends Component {

    @property(TiledMap)
    tiledMap: TiledMap;

    @property({ type: Node })
    dialogNode: Node;

    protected onLoad(): void {
        // PhysicsSystem2D.instance.debugDrawFlags = EPhysics2DDrawFlags.Shape;

    }

    start() {
        //对话框初始化
        this.dialogNode.getComponent(Dialog).init()

        let tiledSize = this.tiledMap.getTileSize();

        let layer = this.tiledMap.getLayer("wall");

        let layerSize = layer.getLayerSize();

        for (let i = 0; i < layerSize.width; i++) {
            for (let j = 0; j < layerSize.height; j++) {
                let tiled = layer.getTiledTileAt(i, j, true);
                if (tiled.grid != 0) {
                    let rigidBody2D = tiled.node.addComponent(RigidBody2D);
                    rigidBody2D.group = 1 << 2;
                    rigidBody2D.type = ERigidBody2DType.Static;
                    rigidBody2D.enabledContactListener = true;
                    rigidBody2D.wakeUp();

                    let collider2d = tiled.node.addComponent(BoxCollider2D);
                    collider2d.offset = v2(tiledSize.width / 2, tiledSize.height / 2);
                    collider2d.group = 1 << 2;
                    collider2d.size = tiledSize;
                    collider2d.apply();
                }
            }
        }
    }

    update(deltaTime: number) {

    }
}


